// 自定义hooks-封装倒计时-提炼hooks
/* eslint-disable react-hooks/exhaustive-deps */
import React, { useEffect, useState, useRef } from 'react'


export default function useCountDown (initCount = 0, callback) {
    const [count, setCount] = useState(initCount)

    const timerId = useRef()
    const start = (data) => {

        setCount(data)
        timerId.current = setInterval(() => {
            setCount((count) => count - 1)
        }, 1000)


    }

    useEffect(() => {
        return () => {
            console.log('..........')
            clearInterval(timerId.current)
        }
    }, [])


    useEffect(() => {
        if (count === 0) {
            clearInterval(timerId.current)
            //  恢复按钮可以点击
            callback && callback()

        }
    }, [count])
    return {
        count,
        start
    }

}


